package com.googlecode.flex_solder.controls  {

  import mx.core.Container;
  import mx.core.UIComponent;
  import mx.core.UITextField;

  /**
   * Class: MultilineLabel
   * This class creates a multi-line label that grows in height to
   * accomodate the text supplied.
   */
  public class MultilineLabel extends Container {
    private var _textLines:Array = null;

    private var _labelWidth:Number;
    private var _labelHeight:Number;
    private var labelX:Number = 0;
    private var labelY:Number = 0;

    private var containerHeight:Number = 0;
    private var containerWidth:Number = 0;

    protected var textHeight:Number;

    public function MultilineLabel() {

    }

    /**
     * Function: setTextLines
     * This function takes the array of _textLines and creates a
     * UITextField to accomodate each entry in the Array. 
     */
    protected function setTextLines():void {
      if (null != _textLines) {
	for (var idx:int = 0; idx < _textLines.length; idx++) {
	  var labelText:String = String(_textLines[idx]);
	  var uiTextField:UITextField = new UITextField();
	  uiTextField.text = labelText;
	  uiTextField.restrict = "";

	  if (!isNaN(_labelWidth)) {
	    uiTextField.explicitWidth = _labelWidth;
	    containerWidth = _labelWidth;
	  }
	  if (!isNaN(_labelHeight)) {
	    uiTextField.explicitHeight = _labelHeight;
	  }
	  uiTextField.x = labelX;
	  uiTextField.y = labelY;
	  this.addChild(uiTextField);
	  uiTextField.validateNow();
	  trace('_labelHeight=' + _labelHeight
		+ ' _labelWidth=' + _labelWidth);
	  trace('labelY=' + labelY + ' text=' + uiTextField.text
		+ ' h=' + uiTextField.height
		+ ' w=' + uiTextField.width
		+ ' x=' + uiTextField.x + ' y=' + uiTextField.y);

	  containerHeight += uiTextField.height;
	  labelY += uiTextField.height;
	  if (containerWidth == 0) {
	    containerWidth = uiTextField.width;
	  }
	}
	this.explicitHeight = containerHeight;
	this.explicitWidth = containerWidth;
	this.validateNow();
	trace(this.uid + ' h=' + this.height + ' w=' + this.width);
      }

    }

    public function setComponentSize():void {
      this.setActualSize(getRealWidth(), getRealHeight());      
    }

    public function getRealHeight():Number {
      var realHeight:Number = this.height;
      if (0 == realHeight) {
	realHeight = this.getExplicitOrMeasuredHeight();
      }

      return realHeight;

    }

    public function getRealWidth():Number {
      var realWidth:Number = this.width;
      if (0 == realWidth) {
	realWidth = this.getExplicitOrMeasuredWidth();
      }

      return realWidth;

    }

    public function get labelHeight():Number {
       return _labelHeight;
    }

    [Bindable]
    public function set labelHeight(labelHeight:Number):void {
       _labelHeight = labelHeight;
    }

    public function get labelWidth():Number {
       return _labelWidth;
    }

    [Bindable]
    public function set labelWidth(labelWidth:Number):void {
       _labelWidth = labelWidth;
    }

    public function get textLines():Array {
       return _textLines;
    }

    [Bindable]
    public function set textLines(textLines:Array):void {
       _textLines = textLines;
       setTextLines();
    }

    public function getInfo():String {
      var controlInfo:String = "";
      controlInfo = this.uid + " x=" + this.x + " y=" + this.y
	+ " eOrmh=" + this.getExplicitOrMeasuredHeight()
	+ " eOrmw=" + this.getExplicitOrMeasuredWidth()
	+ " h=" + this.height
	+ " w=" + this.width
	;
      return controlInfo;
    }

  }
}